package com.ailiwu.www.ailiwu.Fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.adapter.CommentRecyclerViewAdapter;
import com.ailiwu.www.ailiwu.base.BaseFragment;
import com.ailiwu.www.ailiwu.bean.Comment;
import com.ailiwu.www.ailiwu.url.Urls;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;

/**
 * Author:    Yyang
 * Version    V1.0
 * Date:      2015.12.5  9:00
 * Description: 评论页面
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * ${date}          Yyang                1.0                1.0
 * Why & What is modified:
 */
public class CommentFragment extends BaseFragment {
    private RecyclerView recyclerView_comment;
    private int commodityId ;
    private ArrayList<Comment.DataEntity.CommentsEntity> commentsList;
    private String next_url;
    private CommentRecyclerViewAdapter adapter;
    /**
     * 获取一个CommentFragment对象
     * @param CommodityId 对应商品的id
     * @return
     */
    public static CommentFragment newInstance(int CommodityId) {
        CommentFragment fragment = new CommentFragment();
        Bundle args = new Bundle();
        args.putInt("COMMODITYID",CommodityId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments() != null){
            commodityId = getArguments().getInt("COMMODITYID");
        }
    }

    @Override
    protected View inflateView() {
        return View.inflate(getActivity(), R.layout.fragment_comment, null);
    }

    @Override
    protected void initView(View view) {
        recyclerView_comment = (RecyclerView) view.findViewById(R.id.recyView_comment);
        recyclerView_comment.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initData() {
        commentsList = new ArrayList<>();
//        Log.i("oye", "开始加载Json");
        // 使用xUtils的网络模块加载数据
        HttpUtils get = new HttpUtils();
        //设置连接超时时间
        get.configTimeout(100000);
        //设置缓存区中数据的存储时间，单位为毫秒,这里设置为1天.
        get.configDefaultHttpCacheExpiry(24 * 3600 * 1000);
        //设置缓存区的大小，单位是b字节
        get.configHttpCacheSize(4 * 1024 * 1024);

        String url = Urls.commentUrl(commodityId);
        get.send(HttpRequest.HttpMethod.GET, Urls.commentUrl(commodityId), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String Json = responseInfo.result;
//                Log.i("oye",Json.toString());
                Comment comment = gson.fromJson(Json,Comment.class);
                next_url = comment.data.paging.next_url;//下一页的url
                commentsList = comment.data.comments;

                adapter = new CommentRecyclerViewAdapter(getActivity(),commentsList);
                recyclerView_comment.setAdapter(adapter);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.i("oye", "failure:评论加载失败  " + e.getMessage() + "  " + s);
            }
        });

    }

}
